package service

import (
	"github.com/go-admin-team/go-admin-core/sdk/service"
	"tuowei.com/app/admin/models"
	"tuowei.com/app/teacher/service/dto"
	cDto "tuowei.com/common/dto"
)

type ClassService struct {
	service.Service
}

func (c ClassService) GetAllLists(lists *[]models.TeachingPoint, teacherId int) error {
	//Classes
	// 查询当前老师的教学点id
	pointIds := make([]int, 0)
	c.Orm.Table(models.SysTeachingPoint2teacher{}.TableName()).
		Where("enrollment_id = ?", teacherId).
		Pluck("point_id", &pointIds)
	if len(pointIds) == 0 {
		return nil
	}
	return c.Orm.Table(models.TeachingPoint{}.TableName()).
		Preload("Classes").
		Where("id in (?)", pointIds).
		Find(lists).Error

}

func (c ClassService) GetLists(lists *[]models.SysClassList, count *int64, req *dto.GetClassListsRequest, teacherId int) error {
	// 查询当前老师的教学点id
	pointIds := make([]int, 0)
	c.Orm.Table(models.SysTeachingPoint2teacher{}.TableName()).Where("enrollment_id = ?", teacherId).Pluck("point_id", &pointIds)
	if len(pointIds) == 0 {
		return nil
	}
	return c.Orm.Preload("Dept").
		Scopes(
			cDto.Paginate(req.GetPageSize(), req.GetPageIndex()),
			cDto.MakeCondition(req.GetNeedSearch()),
		).
		Where("dept_id in ?", pointIds).
		Find(&lists).Limit(-1).Offset(-1).Count(count).Error
}
